//
//  MainTarBarViewController.m
//  IMChat
//
//  Created by CLSgGhost on 2021/7/15.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "MainTarBarViewController.h"

#define TabbarItemNums 5.0

@interface MainTarBarViewController () <UITabBarDelegate>

@property (nonatomic,assign) NSInteger  indexFlag;

@end

@implementation MainTarBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //会话列表
    TOConversationListViewController *conversationListView = [[TOConversationListViewController alloc] init];
    conversationListView.title = @"消息";
    conversationListView.tabBarItem.image = [UIImage imageNamed:@"xiaoxi.btn"];
    UINavigationController *conversationListNav = [[UINavigationController alloc] initWithRootViewController:conversationListView];
    
    //通讯录好友
    FriendTableViewController *friendView = [[FriendTableViewController alloc]initWithStyle:UITableViewStylePlain];
    friendView.title = @"通讯录";
    friendView.tabBarItem.image = [UIImage imageNamed:@"tongxunlu.btn"];
    UINavigationController *friendNav = [[UINavigationController alloc] initWithRootViewController:friendView];
    
    //我
    MeTableViewController *meView = [[MeTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    meView.title = @"我";
    meView.tabBarItem.image = [UIImage imageNamed:@"me.btn"];
    UINavigationController *meNav = [[UINavigationController alloc] initWithRootViewController:meView];
    
    [self setViewControllers:[NSArray arrayWithObjects:conversationListNav,friendNav,meNav, nil]];
    self.tabBar.tintColor = [UIColor colorWithRed:0.502 green:0.502 blue:1 alpha:1.00];
    
    _indexFlag = 0;
}

//tabBar点击动画
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        //向上移动
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
        //速度控制函数，控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.4;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.removedOnCompletion = YES;
        animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:-10];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];

        self.indexFlag = index;
    }
}

@end
